$NG.AllReady(function (page, { useValuesChange,useDataIndexChange,useUpdateRows,useUpdateRow }) {
    console.log("page is ready");
    console.log($NG.getPageUI());
    console.log($NG.getPageState());
    const mstform = $NG.getCmpApi("p_form_Milestone_Plan_chg_m")
    const dgrid1 = $NG.getCmpApi("p_form_Milestone_Plan_chg_d")
    const dgrid2 = $NG.getCmpApi("p_form_Milestone_Task_chg_d")
    const dgrid3 = $NG.getCmpApi("p_form_Milestone_Repay_chg")
    const dgrid4 = $NG.getCmpApi("p_form_Construction_plan_c")
    const dgrid5 = $NG.getCmpApi("p_form_Income_Plan_chg")
  
    ////////////////////////////////////////////////// 设置按钮属性
    const toolbar = $NG.getCmpApi("toolbar");
    const toolbar_top = $NG.getCmpApi("toolbar_top");
    const toolbar_dgrid1 = $NG.getCmpApi("toolbar_" + "p_form_Milestone_Plan_chg_d");
    const toolbars = [
      { toolbar: toolbar, items: ["add", "copy", "attachment", "imp_push_his", "impinfoview", "batchExportOfAttachments"] },
      { toolbar: toolbar_top, items: ["imp", "impinfoview", "imp_push_his"] },
      { toolbar: toolbar_dgrid1, items: ["", "attachment"] },
    ];
    toolbars.forEach(({ toolbar, items }) => {
      if (toolbar && typeof toolbar.getItem === "function") {
        items.forEach((item) => {
          const button = toolbar.getItem(item);
          if (button) {
            button.setHidden(); // 隐藏按钮
          }
        });
      }
    });
    if(page.oType == 'add'){
        getLcbjhHkDetail()
        getLcbjhSgjhDetail()
        getLcbjhSrjhDetail()
        getLcbjhInfo()
    }
    useValuesChange(({args})=>{
        getLcbjhHkDetail()
        getLcbjhSgjhDetail()
        getLcbjhSrjhDetail()
        getLcbjhInfo()
    },"p_form_Milestone_Plan_chg_m.phid_pc")

    useUpdateRows(({args})=>{
        generate15DigitNumber(args[0])
        dgrid3.refreshView()
    },"p_form_Milestone_Repay_chg")

    useUpdateRows(({args})=>{
        generate15DigitNumber(args[0])
        dgrid4.refreshView()
    },"p_form_Construction_plan_c")

    useUpdateRows(({args})=>{
        generate15DigitNumber(args[0])
        dgrid5.refreshView()
    },"p_form_Income_Plan_chg")
    //////////////////////////////////////////////////
    // 回款明细带出
    function getLcbjhHkDetail(){
        const phid_pc = mstform.getItem("phid_pc").getValue();
        if(!phid_pc) return 
        $NG.execServer("getLcbjhHkDetail",{phid_pc},(res)=>{
            console.log(`回款明细自动带出`,res);
            if(res.count == 0) return 
            const _data = JSON.parse(res.data).reduce((value,item)=>{
                const el = item.extendObjects
                for(let key in el){
                    if(key.includes('exname')){
                        const _key = key.replace('exname','EXName')
                        el[_key] = el[key]
                        delete el[key]
                    }
                }
                el.u_source_phid = el.phid
                delete el.phid
                value.push({...el})
                return value
            },[])
            dgrid3.clearRows().then(()=>{
                dgrid3.addRows(_data)
            })
        })
    }
    //施工计划明细带出
    function getLcbjhSgjhDetail(){
        const phid_pc = mstform.getItem("phid_pc").getValue();
        if(!phid_pc) return 
        $NG.execServer("getLcbjhSgjhDetail",{phid_pc},(res)=>{
            console.log(`施工明细自动带出`,res);
            if(res.count == 0) return 
            const _data = JSON.parse(res.data).reduce((value,item)=>{
                const el = item.extendObjects
                for(let key in el){
                    if(key.includes('exname')){
                        const _key = key.replace('exname','EXName')
                        el[_key] = el[key]
                        delete el[key]
                    }
                }
                el.u_source_phid = el.phid
                delete el.phid
                value.push({...el})
                return value
            },[])
            dgrid4.clearRows().then(()=>{
                dgrid4.addRows(_data)
            })
        })
    }
    //收入计划明细带出
    function getLcbjhSrjhDetail (){
        const phid_pc = mstform.getItem("phid_pc").getValue();
        if(!phid_pc) return 
        $NG.execServer("getLcbjhSrjhDetail",{phid_pc},(res)=>{
            console.log(`收入明细自动带出`,res);
            if(res.count == 0) return 
            const _data = JSON.parse(res.data).reduce((value,item)=>{
                const el = item.extendObjects
                for(let key in el){
                    if(key.includes('exname')){
                        const _key = key.replace('exname','EXName')
                        el[_key] = el[key]
                        delete el[key]
                    }
                }
                el.u_source_phid = el.phid
                delete el.phid
                value.push({...el})
                return value
            },[])
            dgrid5.clearRows().then(()=>{
                dgrid5.addRows(_data)
            })
        })
    }
    // 获取来源主键
    function getLcbjhInfo(){
        const phid_pc = mstform.getItem("phid_pc").getValue();
        if(!phid_pc) return 
        $NG.execServer("getLcbjhInfo",{phid_pc},(res)=>{
            if(res.count == 0) return
            const _data = JSON.parse(res.data)[0].extendObjects
            $NG.updateState((updater)=>{
                updater.data.p_form_Milestone_Plan_chg_m.setProps({
                    u_source_phid:_data.phid
                })
            })
        })
    }
    //生成phid
    function generate15DigitNumber(arr) {
        arr.forEach((item) => {
			let str = "";
			// 获取当前时间戳
			let timestamp = Date.now().toString();
			// 如果时间戳不足 15 位，计算需要补充的位数
			let remainingLength = 15 - timestamp.length;
			if (remainingLength > 0) {
				// 生成指定长度的随机数
				let randomPart = "";
				for (let i = 0; i < remainingLength; i++) {
					randomPart += Math.floor(Math.random() * 10);
				}
				// 将时间戳和随机数拼接成 15 位数字
				str = String(timestamp + randomPart);
			} else {
				// 如果时间戳长度超过 15 位，截取前 15 位
				str = String(timestamp.slice(0, 15));
			}
            console.log(str);
			if (!item.u_phid) {
				item.u_phid = str;
                if(!item.u_source_phid){
                    item.u_source_phid = str;
                }
                dgrid1.refreshView();
				dgrid2.refreshView();
			}
		});
    }
  });
  